var prefix = jsctx + "/hlhtTenantOpenList";
var pilePrefix = jsctx + "/elecPile";

var pileList = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageElecPile",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            elecStationId: $("#elecStationId").val().trim(),
                            operateStatus: $("#operateStatus").val().trim(),
                            pileName: $("#pileName").val().trim(),
                            pileType: $("#pileType").val().trim()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '',width:'50',align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'stationName', title: '所属桩群', width:'100',align: 'left'},
                        {field: 'power', title: '额定功率', width:'50',align: 'left'},
                        {
                            field: 'hasParking', title: '是否有车位', width:'50',align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span>是</span>'
                                } else if (value == '1') {
                                    return '<span>否</span>'
                                }
                            }
                        },
                        {
                            field: 'hasLock', title: '是否有地锁', width:'50',align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span>否</span>'
                                } else if (value == '1') {
                                    return '<span>是</span>'
                                }
                            }
                        },
                        {field: 'pileNo', title: '桩编号', width:'100',align: 'left'},
                        {field: 'pileName', title: '桩名称', width:'100',align: 'left'},
                        {
                            field: 'pileElecType', title: '电桩类型', width:'100',align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '1') {
                                    return '<span>单直流</span>'
                                } else if (value == '2') {
                                    return '<span>单交流</span>'
                                } else if (value == '3') {
                                    return '<span>双直流</span>'
                                } else if (value == '4') {
                                    return '<span>双交流</span>'
                                }
                            }
                        },
                        // {field: 'pileBand', title: '桩品牌', width:'100',align: 'left'},
                        {
                            field: 'pileType', title: '设备类型', width:'100',align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '1') {
                                    return '<span>直流设备</span>'
                                } else if (value == '2') {
                                    return '<span>交流设备</span>'
                                } else if (value == '3') {
                                    return '<span>交直流一体设备</span>'
                                } else if (value == '4') {
                                    return '<span>无线设备</span>'
                                } else if (value == '5') {
                                    return '<span>其他</span>'
                                }
                            }
                        },
                        {
                            field: '', title: '电枪型号', width:'300',align: 'left',
                            formatter: function (value, row, index) {
                                var z = '';

                                for (var i = 0; i < row.elecGunList.length; i++) {

                                    var gunNoLastNum = row.elecGunList[i].gunNo;
                                    var lastNum = gunNoLastNum.substr(gunNoLastNum.length - 1, 1);
                                    var a = row.elecGunList[i].sortNum + '.';
                                    var b =  lastNum + "号电枪";

                                    var c = '<a class="btn btn-success btn-xs ' + has_edit + '" href="#" title="编辑枪"  data-target="#myModal" data-toggle="modal"  onclick="pileList.editElecGun(\''
                                        + row.elecGunList[i].elecGunId
                                        + '\')">编辑枪</a> ';

                                    if (row.elecGunList[i].operateStatus == '1') {
                                        var d = '<a class="btn btn-danger btn-xs ' + has_add + '" href="#" title="隐藏" onclick="pileList.updateGunStatus(\''
                                            + row.elecGunList[i].elecGunId + '\')">隐藏</a> ';
                                    } else if (row.elecGunList[i].operateStatus == '2') {
                                        var d = '<a class="btn btn-info btn-xs ' + has_add + '" href="#" title="显示" onclick="pileList.updateGunStatus(\''
                                            + row.elecGunList[i].elecGunId + '\')">显示</a> ';
                                    }

                                    var e = '<a class="btn btn-success btn-xs ' + has_edit + '" href="#" title="解锁"  data-target="#myModal" data-toggle="modal"  onclick="pileList.sendUnlock(\''
                                        + row.elecGunList[i].elecGunId
                                        + '\')">解锁</a> ';

                                    var q = '<a class="btn btn-primary btn-xs ' + has_edit + '" href="#" title="二维码"   data-target="#myModal" data-toggle="modal"  onclick="pileList.printElecGunQrCode(\''
                                        + row.elecGunList[i].gunNo
                                        + '\')">二维码</a> ';

                                    z += a + ' ' + b + ' ' + c + ' ' + d + ' ' + '' + e + '' + q;

                                }
                                return z;
                            }
                        },
                        {
                            field: '', title: '使用状态', width:'75',align: 'center',
                            formatter: function (value, row, index) {
                                var z = '';
                                for (var i = 0; i < row.elecGunList.length; i++) {
                                    var gunNoLastNum = row.elecGunList[i].gunNo;
                                    var lastNum = gunNoLastNum.substr(gunNoLastNum.length - 1, 1);
                                    if (lastNum == '1') {
                                        if (row.elecGunList[i].status == '0') {
                                            var d = '<a class="btn btn-info btn-xs " href="#">空闲</a> ';
                                        } else if (row.elecGunList[i].status == '1') {
                                            var d = '<a class="btn label-danger btn-xs " href="#">使用中</a> ';
                                        } else if (row.elecGunList[i].status == '2') {
                                            var d = '<a class="btn label-warning btn-xs " href="#">插抢中</a> ';
                                        }
                                        z += d + '<br>';
                                    } else if (lastNum == '2') {
                                        if (row.elecGunList[i].status == '0') {
                                            var d = '<a class="btn btn-info btn-xs " href="#">空闲</a> ';
                                        } else if (row.elecGunList[i].status == '1') {
                                            var d = '<a class="btn label-danger btn-xs " href="#">使用中</a> ';
                                        } else if (row.elecGunList[i].status == '2') {
                                            var d = '<a class="btn label-warning btn-xs " href="#">插抢中</a> ';
                                        }
                                        z += d;
                                    }
                                }
                                if (row.status == 0) {
                                    return '<a class="btn label-danger btn-xs " href="#">离线</a>'
                                } else {
                                    return z;
                                }
                            }
                        },
                        {
                            field: '', title: '电枪状态', width:'75',align: 'center',
                            formatter: function (value, row, index) {
                                var z = '';
                                for (var i = 0; i < row.elecGunList.length; i++) {
                                    var gunNoLastNum = row.elecGunList[i].gunNo;
                                    var lastNum = gunNoLastNum.substr(gunNoLastNum.length - 1, 1);
                                    if (lastNum == '1') {
                                        if (row.elecGunList[i].normalStatus == '0') {
                                            var d = '<a class="btn label-info btn-xs " href="#">正常</a> ';
                                        } else if (row.elecGunList[i].normalStatus == '1') {
                                            var d = '<a class="btn label-warning btn-xs " href="#">枪锁故障</a> ';
                                        } else if (row.elecGunList[i].normalStatus == '255') {
                                            var d = '<a class="btn label-danger btn-xs " href="#">故障</a> ';
                                        }
                                        z += d + '<br>';
                                    } else if (lastNum == '2') {
                                        if (row.elecGunList[i].normalStatus == '0') {
                                            var d = '<a class="btn label-info btn-xs " href="#">正常</a> ';
                                        } else if (row.elecGunList[i].normalStatus == '1') {
                                            var d = '<a class="btn label-warning btn-xs " href="#">枪锁故障</a> ';
                                        } else if (row.elecGunList[i].normalStatus == '255') {
                                            var d = '<a class="btn label-danger btn-xs " href="#">故障</a> ';
                                        }
                                        z += d;
                                    }
                                }
                                return z;
                            }
                        },
                        {
                            field: 'operateStatus', title: '桩运营状态',
                            width:'100',align: 'center',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span class="label label-success">未上线</span>'
                                } else if (value == '1') {
                                    return '<span class="label label-info">上线</span>'
                                } else if (value == '2') {
                                    return '<span class="label label-danger">下线</span>'
                                }
                            }
                        },
                        {
                            title: '操作',
                            field: 'roleId',
                            width:'200',align: 'center',
                            formatter: function (value, row, index) {


                                var a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" onclick="pile.gunListPage(\''
                                    + row.elecPileId
                                    + '\')">枪列表</a> ';

                                var b = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" onclick="pileList.editElecPile(\''
                                    + row.elecPileId
                                    + '\')">编辑桩</a> ';

                                if (row.operateStatus == '0' || row.operateStatus == '2') {
                                    var c = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" onclick="pileList.changeStatus(\''
                                        + row.elecPileId
                                        + '\')">上线</a> ';
                                } else if (row.operateStatus == '1') {
                                    var c = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#"  onclick="pileList.changeStatus(\''
                                        + row.elecPileId
                                        + '\')">下线</a> ';
                                }
                                var d = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#"  onclick="pileList.detailElecPile(\''
                                    + row.elecPileId
                                    + '\')">详情</a> ';

                                var e = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#"  onclick="pile.deleteElecPile(\''
                                    + row.elecPileId
                                    + '\')">删除</a> ';


                                // return d + a + b + c + e;
                                return d + b + c;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function addElecPileByStation(elecStationId) {
        location.href = jsctx + "/elecStation/addElecPileByStation/" + elecStationId;
    }

    function addElecPile() {
        location.href = prefix + "/addElecPile";
    }

    function editElecPile(elecPileId) {
        location.href = pilePrefix + "/editElecPile/" + elecPileId;
    }

    function detailElecPile(elecPileId) {
        location.href = prefix + "/detailElecPile/" + elecPileId;
    }

    function gunListPage(elecPileId) {
        location.href = prefix + "/elecGun/" + elecPileId;
    }

    function pileFailure() {
        location.href = prefix + "/pileFailure";
    }

    function editElecGun(elecGunId) {
        location.href = pilePrefix + "/editElecGun/" + elecGunId;
    }

    function detailElecPile(elecPileId) {
        location.href = pilePrefix + "/detailElecPile/" + elecPileId;
    }

    function updateGunStatus(elecGunId) {
        layer.confirm('确定要变更状态？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: pilePrefix + "/updateGunStatus",
                type: "post",
                data: {
                    'elecGunId': elecGunId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("变更成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function changeStatus(elecPileId) {
        layer.confirm('确定要变更状态？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/updateStatus",
                type: "post",
                data: {
                    'elecPileId': elecPileId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("变更成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function deleteElecPile(elecPileId) {
        layer.confirm('确定要删除选中的记录(对应枪同时被删除)？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/deleteElecPile",
                type: "post",
                data: {
                    'elecPileId': elecPileId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    function goBack() {
        window.history.back();
    }

    function changeStatus(elecPileId) {
        layer.confirm('确定要变更状态？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: pilePrefix + "/updateStatus",
                type: "post",
                data: {
                    'elecPileId': elecPileId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("变更成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }



    function updateGunStatus(elecGunId) {
        layer.confirm('确定要变更状态？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: pilePrefix + "/updateGunStatus",
                type: "post",
                data: {
                    'elecGunId': elecGunId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("变更成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function sendUnlock(elecGunId) {
        layer.confirm('确定解锁此枪？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/sendUnlock",
                type: "post",
                data: {
                    'elecGunId': elecGunId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("解锁成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function printElecGunQrCode(gunNo) {
        // location.href = prefix + "/genElecGunQrCode/" + gunNo;
        layer.open({
            type: 2,
            title: '枪号：' + gunNo,
            shadeClose: false,
            area: ['350px', '300px'],
            content: pilePrefix + '/genElecGunQrCode/' + gunNo
        });
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        changeStatus:changeStatus,
        editElecGun:editElecGun,
        updateGunStatus:updateGunStatus,
        sendUnlock:sendUnlock,
        printElecGunQrCode:printElecGunQrCode,
        detailElecPile: detailElecPile,
        editElecPile:editElecPile
    };
})();

